﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class TransportEquipmentDA : ITransportEquipmentDA
    {
        private string CARRIER_ID = "@CARRIER_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string EQUIPMENT_TYPE_ID = "@EQUIPMENT_TYPE_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_TRANSPORT_EQUIPMENT_ID_UNIQUE = " SELECT COUNT(1) FROM @_@TRANSPORT_EQUIPMENT WHERE TRANSPORT_EQUIPMENT_ID = @TRANSPORT_EQUIPMENT_ID ";
        private string SQL_DELETE_TRANSPORT_EQUIPMENT = " DELETE FROM @_@TRANSPORT_EQUIPMENT WHERE TRANSPORT_EQUIPMENT_ID = @TRANSPORT_EQUIPMENT_ID ";
        private string SQL_INSERT_TRANSPORT_EQUIPMENT = " INSERT INTO @_@TRANSPORT_EQUIPMENT ( WH_ID, TRANSPORT_EQUIPMENT_ID, CARRIER_ID, EQUIPMENT_TYPE_ID, STATUS, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @TRANSPORT_EQUIPMENT_ID, @CARRIER_ID, @EQUIPMENT_TYPE_ID, @STATUS, @REMARK, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_TRANSPORT_EQUIPMENT = "SELECT TE.ROWID, TE.WH_ID, TE.TRANSPORT_EQUIPMENT_ID, TE.CARRIER_ID, TE.EQUIPMENT_TYPE_ID,  TE.STATUS AS STATUS,   T.[TEXT] AS TE_STATUS,  TE.REMARK, TE.IS_ACTIVE, TE.TS, TE.CREATED_BY, TE.CREATED_DATE, TE.UPDATED_BY, TE.UPDATED_DATE  FROM         @_@TRANSPORT_EQUIPMENT AS TE  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = TE.STATUS AND SCD.SYS_CODE_TYPE = 'EquipmentStatus'    LEFT JOIN sysadm.TRANSLATION T ON 'EquipmentStatus.'+ TE.STATUS = T.ITEM_ID          AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_TRANSPORT_EQUIPMENT = " UPDATE @_@TRANSPORT_EQUIPMENT SET WH_ID = @WH_ID, CARRIER_ID = @CARRIER_ID, EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID, STATUS = @STATUS, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE TRANSPORT_EQUIPMENT_ID = @TRANSPORT_EQUIPMENT_ID  ";
        private string STATUS = "@STATUS";
        private string TRANSPORT_EQUIPMENT_ID = "@TRANSPORT_EQUIPMENT_ID";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckTransportEquipmentIDUnique(string transportEquipmentID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportEquipmentID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_TRANSPORT_EQUIPMENT_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckTransportEquipmentIDUnique(DataBase dataBase, DbTransaction tran, string transportEquipmentID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportEquipmentID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_TRANSPORT_EQUIPMENT_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteTransportEquipment(List<string> transportEquipmentID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < transportEquipmentID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportEquipmentID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteTransportEquipment(DataBase dataBase, DbTransaction tran, List<string> transportEquipmentID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < transportEquipmentID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportEquipmentID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllTransportEquipment(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql, paras);
        }

        public DataSet GetAllTransportEquipment(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql, paras);
        }

        public TransportEquipmentInfo GetTransportEquipmentByID(string transportequipmentID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID) + " WHERE TRANSPORT_EQUIPMENT_ID = @TRANSPORT_EQUIPMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportequipmentID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            TransportEquipmentInfo transportEquipmentInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    transportEquipmentInfo = new TransportEquipmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["TRANSPORT_EQUIPMENT_ID"].ToString(), reader["CARRIER_ID"].ToString(), reader["EQUIPMENT_TYPE_ID"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return transportEquipmentInfo;
        }

        public TransportEquipmentInfo GetTransportEquipmentByID(DataBase dataBase, DbTransaction tran, string transportequipmentID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID) + " WHERE TRANSPORT_EQUIPMENT_ID = @TRANSPORT_EQUIPMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@TRANSPORT_EQUIPMENT_ID", transportequipmentID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            TransportEquipmentInfo transportEquipmentInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                transportEquipmentInfo = new TransportEquipmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["TRANSPORT_EQUIPMENT_ID"].ToString(), reader["CARRIER_ID"].ToString(), reader["EQUIPMENT_TYPE_ID"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return transportEquipmentInfo;
        }

        public DataSet GetTransportEquipmentByQueryList(List<string> sqlWhere, TransportEquipmentQueryEntity transportequipmentQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!transportequipmentQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, transportequipmentQuery.CurrentPage, transportequipmentQuery.PageSize, transportequipmentQuery.SortField, transportequipmentQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetTransportEquipmentByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, TransportEquipmentQueryEntity transportequipmentQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (transportequipmentQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, transportequipmentQuery.CurrentPage, transportequipmentQuery.PageSize, transportequipmentQuery.SortField, transportequipmentQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertTransportEquipment(TransportEquipmentInfo transportEquipmentInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    transportEquipmentInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_TransportEquipment_Parameters(transportEquipmentInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertTransportEquipment(DataBase dataBase, DbTransaction tran, TransportEquipmentInfo transportEquipmentInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    transportEquipmentInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_TransportEquipment_Parameters(transportEquipmentInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_TransportEquipment_Parameters(TransportEquipmentInfo transportEquipmentInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.TRANSPORT_EQUIPMENT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CARRIER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.EQUIPMENT_TYPE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(transportEquipmentInfo.WhID))
            {
                paramArray[0].Value = transportEquipmentInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = transportEquipmentInfo.TransportEquipmentID;
            if (!string.IsNullOrEmpty(transportEquipmentInfo.CarrierID))
            {
                paramArray[2].Value = transportEquipmentInfo.CarrierID;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transportEquipmentInfo.EquipmentTypeID))
            {
                paramArray[3].Value = transportEquipmentInfo.EquipmentTypeID;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transportEquipmentInfo.Status))
            {
                paramArray[4].Value = transportEquipmentInfo.Status;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transportEquipmentInfo.Remark))
            {
                paramArray[5].Value = transportEquipmentInfo.Remark;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(transportEquipmentInfo.IsActive))
            {
                paramArray[6].Value = transportEquipmentInfo.IsActive;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = transportEquipmentInfo.CreatedBy;
            paramArray[8].Value = transportEquipmentInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateTransportEquipment(TransportEquipmentInfo transportEquipmentInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    transportEquipmentInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_TransportEquipment_Parameters(transportEquipmentInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateTransportEquipment(DataBase dataBase, DbTransaction tran, TransportEquipmentInfo transportEquipmentInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    transportEquipmentInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_TransportEquipment_Parameters(transportEquipmentInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TRANSPORT_EQUIPMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

